(function($, doc) {
	$.init();
	$.ready(function() {
		
	});
	
	//初始化滚动条
	$('#scroll1').scroll({
		indicators: true //是否显示滚动条
	});
	//现场整改
	mui(".mui-content").on("tap","#pass",function(){
		if(this.querySelector("input").getAttribute("checked")){
			this.querySelector("input").removeAttribute("checked")
		}else{
			this.querySelector("input").setAttribute("checked",true)
		};
		return false;
	})

    //首页
	mui(".mui-bar").on("tap", ".com-to-home-btn", function () {
	    location.href = "home.html";
	});


	//更多
	mui(".mui-content").on("tap",".more",function(){
		document.querySelector(".more span").classList.toggle("mui-icon-arrowdown");
		document.querySelector(".more span").classList.toggle("mui-icon-arrowup");
		document.querySelector(".more_one").classList.toggle("mui-hidden");
		document.querySelector(".more_two").classList.toggle("mui-hidden");
	});
	
	//照片
	mui(".mui-content").on("tap","#photo",function(){
		if(mui.os.plus){
			var a = [{
				title: "拍照"
			}, {
				title: "从手机相册选择"
			}];
			plus.nativeUI.actionSheet({
//						title: "修改头像",
				cancel: "取消",
				buttons: a
			}, function(b) {
				switch (b.index) {
//							case 0:
//								break;
					case 1:
						getImage();
						break;
					case 2:
						galleryImg();
						break;
					default:
						break
				}
			})	
		}
		var a=document.querySelectorAll("textarea");
		a[0].blur();
		return false;
	});
	
	function getImage() {
		var c = plus.camera.getCamera();
		c.captureImage(function(e) {
			plus.io.resolveLocalFileSystemURL(e, function(entry) {
				var s = entry.toLocalURL() + "?version=" + new Date().getTime();
				console.log(s);
				var cloneDom=document.querySelector("#photo_template").cloneNode(true);
				cloneDom.querySelector("img").setAttribute("src",s);
				document.querySelector("#photo_template").parentNode.insertBefore(cloneDom,document.querySelector("#photo"));
				//变更大图预览的src
				//目前仅有一张图片，暂时如此处理，后续需要通过标准组件实现
				document.querySelector("#__mui-imageview__group .mui-slider-item img").src = s + "?version=" + new Date().getTime();
			}, function(e) {
				console.log("读取拍照文件错误：" + e.message);
			});
		}, function(s) {
			console.log("error" + s);
		}, {
			filename: "_doc/head.jpg"
		})
	}

	function galleryImg() {
		plus.gallery.pick(function(a) {
			plus.io.resolveLocalFileSystemURL(a, function(entry) {
				plus.io.resolveLocalFileSystemURL("_doc/", function(root) {
					root.getFile("head.jpg", {}, function(file) {
						//文件已存在
						file.remove(function() {
							console.log("file remove success");
							entry.copyTo(root, 'head.jpg', function(e) {
									var e = e.fullPath + "?version=" + new Date().getTime();
									document.getElementById("head-img").src = e;
									document.getElementById("head-img1").src = e;
									//变更大图预览的src
									//目前仅有一张图片，暂时如此处理，后续需要通过标准组件实现
									document.querySelector("#__mui-imageview__group .mui-slider-item img").src = e + "?version=" + new Date().getTime();;
								},
								function(e) {
									console.log('copy image fail:' + e.message);
								});
						}, function() {
							console.log("delete image fail:" + e.message);
						});
					}, function() {
						//文件不存在
						entry.copyTo(root, 'head.jpg', function(e) {
								var path = e.fullPath + "?version=" + new Date().getTime();
								document.getElementById("head-img").src = path;
								document.getElementById("head-img1").src = path;
								//变更大图预览的src
								//目前仅有一张图片，暂时如此处理，后续需要通过标准组件实现
								document.querySelector("#__mui-imageview__group .mui-slider-item img").src = path;
							},
							function(e) {
								console.log('copy image fail:' + e.message);
							});
					});
				}, function(e) {
					console.log("get _www folder fail");
				})
			}, function(e) {
				console.log("读取拍照文件错误：" + e.message);
			});
		}, function(a) {}, {
			filter: "image"
		})
	};
	
	function defaultImg() {
		if(mui.os.plus){
			plus.io.resolveLocalFileSystemURL("_doc/head.jpg", function(entry) {
				var s = entry.fullPath + "?version=" + new Date().getTime();;
				document.querySelector(".img").src = s;
				document.querySelector("head-img1").src = s;
			}, function(e) {
				document.querySelector("img.img").src = '../img/tab_index_focus.png';
			})
		}else{
			document.querySelector("img.img").src = '../img/tab_index_focus.png';
		}
	}
	setTimeout(function () {
//		defaultImg();
		setTimeout(function() {
			initImgPreview();
		}, 300);
	},500);
	//图片预览
	function initImgPreview() {
		var imgs = document.querySelectorAll("img.img");
		imgs = mui.slice.call(imgs);
		if (imgs && imgs.length > 0) {
			var slider = document.createElement("div");
			slider.setAttribute("id", "__mui-imageview__");
			slider.classList.add("mui-slider");
			slider.classList.add("mui-fullscreen");
			slider.style.display = "none";
			slider.addEventListener("tap", function() {
				slider.style.display = "none";
				setTimeout(function(){
					var a=document.querySelectorAll("textarea");
						a[0].blur();
				},4) 
				event.stopPropagation();
				event.preventDefault();
				return false;
			});
			slider.addEventListener("touchmove", function(event) {
				event.preventDefault();
			})
			var slider_group = document.createElement("div");
			slider_group.setAttribute("id", "__mui-imageview__group");
			slider_group.classList.add("mui-slider-group");
			imgs.forEach(function(value, index, array) {
				//给图片添加点击事件，触发预览显示；
				value.addEventListener('tap', function() {
					slider.style.display = "block";
					_slider.refresh();
					_slider.gotoItem(index, 0);
				})
				var item = document.createElement("div");
				item.classList.add("mui-slider-item");
				var a = document.createElement("a");
				var img = document.createElement("img");
				img.setAttribute("src", value.src);
				a.appendChild(img)
				item.appendChild(a);
				slider_group.appendChild(item);
			});
			slider.appendChild(slider_group);
			document.body.appendChild(slider);
			var _slider = mui(slider).slider();
		}
	}
	
})(mui, document);